package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.utils.R;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class FileUploadController {
    @ResponseBody
//    @RequestMapping(value="/employerManager/uploadFile" ,produces="text/html;charset=utf-8" )
    @RequestMapping(value="/employerManager/uploadFile" )
    public  R importPicFile1(@RequestParam("excelfile")MultipartFile file1){
        if(file1.isEmpty()){
            System.out.println("空文件");
        } else{
            String originalFilename=file1.getOriginalFilename();
            String fileBaseName= FilenameUtils.getBaseName(originalFilename);
            Date now = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String floderName=fileBaseName+"_" +df.format(now);
            try{
                //创建要上传的路径
                File fdir = new File("/home/yanglei/mystudy/javaWorkSpace/hr_manager/src/main/resources/excel");
                if (!fdir.exists()) {
                    fdir.mkdirs();
                }
                //文件上传到路径下
                FileUtils. copyInputStreamToFile(file1.getInputStream(), new File(fdir,originalFilename));
                //coding
                System.out.println("成功");

            } catch (Exception e) {

                return R.ok("上传异常");
            }
        }
        return R.ok("上传成功");
}
}
